﻿Imports TransportadoraToledoDAL

Public Class historicoCargasBLL

#Region "Propriedades"

    Private codigo As Integer
    Public Property pCodigo() As Integer
        Get
            Return codigo
        End Get
        Set(ByVal value As Integer)
            codigo = value
        End Set
    End Property



    Private codCarga As Integer
    Public Property pCodCarga() As Integer
        Get
            Return codCarga
        End Get
        Set(ByVal value As Integer)
            codCarga = value
        End Set
    End Property


    Private data As Date
    Public Property pData() As Date
        Get
            Return data
        End Get
        Set(ByVal value As Date)
            data = value
        End Set
    End Property


    Private descricao As String
    Public Property pDescricao() As String
        Get
            Return descricao
        End Get
        Set(ByVal value As String)
            descricao = value
        End Set
    End Property


    Private protocolo As String
    Public Property pProtocolo() As String
        Get
            Return protocolo
        End Get
        Set(ByVal value As String)
            protocolo = value
        End Set
    End Property
#End Region

#Region "Construtores"

    Sub New()

    End Sub

    Sub New(ByVal codigo As Integer, ByVal carga As Integer, ByVal data As Date, ByVal descricao As String)
        Me.codigo = codigo
        Me.codCarga = carga
        Me.data = data
        Me.descricao = descricao
    End Sub

#End Region

#Region "Métodos"
    Sub inserir()
        Dim objDAL As New historicoCargasDAL
        Try
            If (codCarga = 0) Then
                Throw New Exception("Preencha o Campo Carga")
            ElseIf (Not IsDate(data)) Then
                Throw New Exception("Preencha o Campo Data")
            ElseIf (descricao = "") Then
                Throw New Exception("Preencha o Campo Descrição")
            Else
                Me.codigo = objDAL.inserir(codCarga, data, descricao)
            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
    End Sub

    Sub alterar()
        Dim objDAL As New historicoCargasDAL
        Try
            If (codigo = 0) Then
                Throw New Exception("Preencha um Histórico")
            ElseIf (codCarga = 0) Then
                Throw New Exception("Preencha o Campo Carga")
            ElseIf (Not IsDate(data)) Then
                Throw New Exception("Preencha o Campo Data")
            ElseIf (descricao = "") Then
                Throw New Exception("Preencha o Campo Descrição")
            Else
                objDAL.alterar(codigo, codCarga, data, descricao)
            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
    End Sub

    Sub excluir()
        Dim objDAL As New historicoCargasDAL
        Try
            objDAL.excluir(codigo)
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
    End Sub

    Function localizarCarga(ByVal descricao As String) As DataTable
        Dim tab As DataTable
        Dim objDAL = New historicoCargasDAL
        Try
            tab = objDAL.localizarCarga(descricao)
            If (tab.Rows.Count > 0) Then
                Me.codigo = tab.Rows(0)(0).ToString
                Me.codCarga = tab.Rows(0)(1).ToString
                Me.data = tab.Rows(0)(2).ToString
                Me.descricao = tab.Rows(0)(3).ToString
            Else
                limpar()
            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
        Return tab
    End Function

    Function localizar(ByVal descricao As String) As DataTable
        Dim tab As DataTable
        Dim objDAL = New historicoCargasDAL
        Try
            tab = objDAL.localizar(descricao)
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
        Return tab
    End Function

    Function localizar(ByVal codigo As Integer) As DataTable
        Dim tab As DataTable
        Dim objDAL = New historicoCargasDAL
        Try
            tab = objDAL.localizar(codigo)
            If (tab.Rows.Count > 0) Then
                Me.codigo = tab.Rows(0)(0).ToString
                Me.codCarga = tab.Rows(0)(4).ToString
                Me.data = tab.Rows(0)(2).ToString
                Me.descricao = tab.Rows(0)(3).ToString
                Me.pProtocolo = tab.Rows(0)(1).ToString
            Else
                limpar()
            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
        Return tab
    End Function

    'retorna todas as linhas pesquisadas da tabela
    Function localizarPrimeiroUltimo(ByVal nome As String) As Integer
        Dim tab As DataTable = Nothing
        Dim objDAL As historicoCargasDAL
        Dim codigo As Integer = 0
        Try
            If (nome = "ultimo") Then
                objDAL = New historicoCargasDAL
                tab = objDAL.localizar(nome)
                If (tab.Rows(0)("hc_codigo").ToString <> "") Then
                    codigo = tab.Rows(0)("hc_codigo").ToString
                End If
            ElseIf (nome = "primeiro") Then
                objDAL = New historicoCargasDAL
                tab = objDAL.localizar(nome)
                If (tab.Rows(0)("hc_codigo").ToString <> "") Then
                    codigo = tab.Rows(0)("hc_codigo").ToString
                End If
            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
        Return codigo
    End Function

    Function localizarProxAnteriro(ByVal nome As String, ByVal codigo As Integer) As Integer
        Dim tab As DataTable = Nothing
        Dim objDAL As historicoCargasDAL
        Dim CodigoP As Integer = 0
        Try
            objDAL = New historicoCargasDAL
            tab = objDAL.localizarProxAnterior(nome, codigo)
            If (tab.Rows.Count > 0) Then
                CodigoP = tab.Rows(0)("hc_codigo").ToString
            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
        Return CodigoP
    End Function

    Sub limpar()
        Me.codigo = Nothing
        Me.data = Nothing
        Me.descricao = Nothing
        Me.codCarga = Nothing
    End Sub

#End Region
End Class
